angular.module("module.print",[])
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider
            .state('home.print',{
                url:"/print",
                templateUrl:"print/print.html",
                controller:'PrintController',
                controllerAs:'vm'
            })
    }])
    .controller('PrintController',[function () {
        var vm=this;
        function loadpage(){
            $.getJSON('printList',{shopId:shopId},function (res) {
                console.log("res",res);
                data=res.data;
                $('.container-fluid').html(template('list_tmpl', data));
                $('.edit').on('click', function() {
                    var i = $(this).attr('data-i');
                    var str = i ? '编辑' : '新增';
                    winAlert({title:str+'打印机',body:template('edit_tmpl', i ? data.dataList[i] : {})});
                    checkfrom(function(para){
                        console.log("para",para);
                        para.shopId=shopId;
                        $.post('printSave', para, function(rs){
                            try {rs=JSON.parse(rs);}catch(e){}
                            if (!rs.succeed) return toast((para.id?'编辑':'添加')+'失败，失败原因：'+rs.message);
                            $('.winAlert').remove();
                            toast('操作成功,重新加载中...', 'success', function(){
                                location.reload();
                            });
                        })
                    }, $('.winAlert .from_table'), $('.winAlert .sure'));
                });

                $('.print').on('click', function() {
                    var i = $(this).attr('data-i');
                    var vo = data.dataList[i];
                    winAlert({
                        body:'您确定要测试打印么',
                        cancel:'取消',
                        fun:function(){
                            return $.getJSON('printTest', {id:vo.id}, function(rs){
                                if (!rs.succeed) return toast('打印失败，失败原因：'+rs.message);
                                toast('打印成功...', 'success');
                            });
                        }});
                });
            });
        }
        loadpage();
    }]);